home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / spsdf.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.0 KB  |  269 lines

  1. /* Copyright (C) 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: spsdf.c,v 1.4 2000/09/19 19:00:51 lpd Exp $ */
  20. /* Common utilities for PostScript and PDF format printing */
  21. #include "stdio_.h"        /* for stream.h */
  22. #include "string_.h"
  23. #include "gstypes.h"
  24. #include "gsmemory.h"
  25. #include "gserror.h"
  26. #include "gserrors.h"
  27. #include "spprint.h"
  28. #include "spsdf.h"
  29. #include "stream.h"
  30. #include "strimpl.h"
  31. #include "sa85x.h"
  32. #include "sstring.h"
  33. #include "scanchar.h"
  34.  
  35. /*
  36.  * Write a string in its shortest form ( () or <> ).  Note that
  37.  * this form is different depending on whether binary data are allowed.
  38.  * Currently we don't support ASCII85 strings ( <~ ~> ).
  39.  */
  40. void
  41. s_write_ps_string(stream * s, const byte * str, uint size, int print_ok)
  42. {
  43.     uint added = 0;
  44.     uint i;
  45.     const stream_template *template;
  46.     stream_AXE_state state;
  47.     stream_state *st = NULL;
  48.  
  49.     if (print_ok & PRINT_BINARY_OK) {
  50.     /* Only need to escape (, ), \, CR, EOL. */
  51.     pputc(s, '(');
  52.     for (i = 0; i < size; ++i) {
  53.         byte ch = str[i];
  54.  
  55.         switch (ch) {
  56.         case char_CR:
  57.             pputs(s, "\\r");
  58.             continue;
  59.         case char_EOL:
  60.             pputs(s, "\\n");
  61.             continue;
  62.         case '(':
  63.         case ')':
  64.         case '\\':
  65.             pputc(s, '\\');
  66.         }
  67.         pputc(s, ch);
  68.     }
  69.     pputc(s, ')');
  70.     return;
  71.     }
  72.     for (i = 0; i < size; ++i) {
  73.     byte ch = str[i];
  74.  
  75.     if (ch == 0 || ch >= 127)
  76.         added += 3;
  77.     else if (strchr("()\\\n\r\t\b\f", ch) != 0)
  78.         ++added;
  79.     else if (ch < 32)
  80.         added += 3;
  81.     }
  82.  
  83.     if (added < size || (print_ok & PRINT_HEX_NOT_OK)) {
  84.     /* More efficient, or mandatory, to represent as PostScript string. */
  85.     template = &s_PSSE_template;
  86.     pputc(s, '(');
  87.     } else {
  88.     /* More efficient, and permitted, to represent as hex string. */
  89.     template = &s_AXE_template;
  90.     st = (stream_state *) & state;
  91.     s_AXE_init_inline(&state);
  92.     pputc(s, '<');
  93.     }
  94.  
  95.     {
  96.     byte buf[100];        /* size is arbitrary */
  97.     stream_cursor_read r;
  98.     stream_cursor_write w;
  99.     int status;
  100.  
  101.     r.ptr = str - 1;
  102.     r.limit = r.ptr + size;
  103.     w.limit = buf + sizeof(buf) - 1;
  104.     do {
  105.         /* One picky compiler complains if we initialize to buf - 1. */
  106.         w.ptr = buf;  w.ptr--;
  107.         status = (*template->process) (st, &r, &w, true);
  108.         pwrite(s, buf, (uint) (w.ptr + 1 - buf));
  109.     }
  110.     while (status == 1);
  111.     }
  112. }
  113.  
  114. /* Set up a write stream that just keeps track of the position. */
  115. int
  116. s_alloc_position_stream(stream ** ps, gs_memory_t * mem)
  117. {
  118.     stream *s = *ps = s_alloc(mem, "s_alloc_position_stream");
  119.  
  120.     if (s == 0)
  121.     return_error(gs_error_VMerror);
  122.     swrite_position_only(s);
  123.     return 0;
  124. }
  125.  
  126. /* ---------------- Parameter printing ---------------- */
  127.  
  128. private_st_printer_param_list();
  129. const param_printer_params_t param_printer_params_default = {
  130.     param_printer_params_default_values
  131. };
  132.  
  133. /* We'll implement the other printers later if we have to. */
  134. private param_proc_xmit_typed(param_print_typed);
  135. /*private param_proc_begin_xmit_collection(param_print_begin_collection); */
  136. /*private param_proc_end_xmit_collection(param_print_end_collection); */
  137. private const gs_param_list_procs printer_param_list_procs = {
  138.     param_print_typed,
  139.     NULL /* begin_collection */ ,
  140.     NULL /* end_collection */ ,
  141.     NULL /* get_next_key */ ,
  142.     gs_param_request_default,
  143.     gs_param_requested_default
  144. };
  145.  
  146. int
  147. s_init_param_printer(printer_param_list_t *prlist,
  148.              const param_printer_params_t * ppp, stream * s)
  149. {
  150.     gs_param_list_init((gs_param_list *)prlist, &printer_param_list_procs,
  151.                NULL);
  152.     prlist->strm = s;
  153.     prlist->params = *ppp;
  154.     prlist->any = false;
  155.     return 0;
  156. }
  157. int
  158. s_alloc_param_printer(gs_param_list ** pplist,
  159.               const param_printer_params_t * ppp, stream * s,
  160.               gs_memory_t * mem)
  161. {
  162.     printer_param_list_t *prlist =
  163.     gs_alloc_struct(mem, printer_param_list_t, &st_printer_param_list,
  164.             "s_alloc_param_printer");
  165.     int code;
  166.  
  167.     *pplist = (gs_param_list *)prlist;
  168.     if (prlist == 0)
  169.     return_error(gs_error_VMerror);
  170.     code = s_init_param_printer(prlist, ppp, s);
  171.     prlist->memory = mem;
  172.     return code;
  173. }
  174.  
  175. void
  176. s_release_param_printer(printer_param_list_t *prlist)
  177. {
  178.     if (prlist) {
  179.     if (prlist->any && prlist->params.suffix)
  180.         pputs(prlist->strm, prlist->params.suffix);
  181.     }
  182. }
  183. void
  184. s_free_param_printer(gs_param_list * plist)
  185. {
  186.     if (plist) {
  187.     printer_param_list_t *const prlist = (printer_param_list_t *) plist;
  188.  
  189.     s_release_param_printer(prlist);
  190.     gs_free_object(prlist->memory, plist, "s_free_param_printer");
  191.     }
  192. }
  193.  
  194. private int
  195. param_print_typed(gs_param_list * plist, gs_param_name pkey,
  196.           gs_param_typed_value * pvalue)
  197. {
  198.     printer_param_list_t *const prlist = (printer_param_list_t *)plist;
  199.     stream *s = prlist->strm;
  200.  
  201.     if (!prlist->any) {
  202.     if (prlist->params.prefix)
  203.         pputs(s, prlist->params.prefix);
  204.     prlist->any = true;
  205.     }
  206.     if (prlist->params.item_prefix)
  207.     pputs(s, prlist->params.item_prefix);
  208.     pprints1(s, "/%s", pkey);
  209.     switch (pvalue->type) {
  210.     case gs_param_type_null:
  211.         pputs(s, " null");
  212.         break;
  213.     case gs_param_type_bool:
  214.         pputs(s, (pvalue->value.b ? " true" : " false"));
  215.         break;
  216.     case gs_param_type_int:
  217.         pprintd1(s, " %d", pvalue->value.i);
  218.         break;
  219.     case gs_param_type_long:
  220.         pprintld1(s, " %l", pvalue->value.l);
  221.         break;
  222.     case gs_param_type_float:
  223.         pprintg1(s, " %g", pvalue->value.f);
  224.         break;
  225.     case gs_param_type_string:
  226.         s_write_ps_string(s, pvalue->value.s.data, pvalue->value.s.size,
  227.                   prlist->params.print_ok);
  228.         break;
  229.     case gs_param_type_name:
  230.         /****** SHOULD USE #-ESCAPES FOR PDF ******/
  231.         pputc(s, '/');
  232.         pwrite(s, pvalue->value.n.data, pvalue->value.n.size);
  233.         break;
  234.     case gs_param_type_int_array:
  235.         {
  236.         uint i;
  237.         char sepr = (pvalue->value.ia.size <= 10 ? ' ' : '\n');
  238.  
  239.         pputc(s, '[');
  240.         for (i = 0; i < pvalue->value.ia.size; ++i) {
  241.             pprintd1(s, "%d", pvalue->value.ia.data[i]);
  242.             pputc(s, sepr);
  243.         }
  244.         pputc(s, ']');
  245.         }
  246.         break;
  247.     case gs_param_type_float_array:
  248.         {
  249.         uint i;
  250.         char sepr = (pvalue->value.fa.size <= 10 ? ' ' : '\n');
  251.  
  252.         pputc(s, '[');
  253.         for (i = 0; i < pvalue->value.fa.size; ++i) {
  254.             pprintg1(s, "%g", pvalue->value.fa.data[i]);
  255.             pputc(s, sepr);
  256.         }
  257.         pputc(s, ']');
  258.         }
  259.         break;
  260.         /*case gs_param_type_string_array: */
  261.         /*case gs_param_type_name_array: */
  262.     default:
  263.         return_error(gs_error_typecheck);
  264.     }
  265.     if (prlist->params.item_suffix)
  266.     pputs(s, prlist->params.item_suffix);
  267.     return 0;
  268. }
  269.